<?php
namespace app\shop\home;

use app\shop\model\Address;
use app\shop\model\Banner;
use app\shop\model\Artwork;
use app\shop\model\Artist;
use app\shop\model\Love;
use app\shop\model\User;
use app\shop\model\Order as OrderModel;
use app\shop\model\AccountLog;
use think\Cache;
use app\shop\model\ArtworkGroup;
use app\shop\model\Artwork as ArtworkModel;

use app\admin\model\Attachment;
/**
 * 个人中心
 */
class Account extends Common
{

	public function _initialize()
	{
		parent::_initialize();
		$userInfo = User::where('id',session("auction_user")['id'])->find();
		if(isset($userInfo->money) && !$userInfo->money){
            $userInfo->money = '0.00';
        }
		$this->assign('userInfo', $userInfo);
	}

	public function index()
	{
        if(isMobile()){
            return $this->fetch('Mobile/Account/index');
        }else{
            return $this->fetch();
        }
	}

	public function pay()
	{
        if(isMobile()){
            return $this->fetch('Mobile/Account/pay');
        }else{
            return $this->fetch();
        }
	}

	public function ajaxPay()
    {
    
		$account = AccountLog::create([
					'money' => request()->param('money'),
					'user_id' => session('auction_user')['id'],
					'type' => 1,
					'status' => 1,
					'remark' => '充值'.request()->param('money').'元'
				]);
		if($account){
			return json(['code'=>0, 'msg'=>'提交成功,等待审核']);
		}else{
			return json(['code'=>1, 'msg'=>'提交失败，联系管理员']);
		}
        
    }

     public function money()
    {
        
       $money = AccountLog::where('user_id', session('auction_user')['id'])->order('sort')->select();
       $state = array(0=>'未知',1=>'待审核',2=>'审核通过',3=>'审核不通过');
       $this->assign('state', $state);
       $this->assign('moneys', $money);

        if(isMobile()){
            return $this->fetch('Mobile/Account/money');
        }else{
            return $this->fetch();
        }
    }

    public function userinformation()
    {

        if(isMobile()){
            return $this->fetch('Mobile/Account/userinformation');
        }else{
            return $this->fetch();
        }
    }

    public function ajaxUserinformation()
    {
    	extract(request()->param());
    	$isUser = User::where('id', session('auction_user')['id'])->update([
		    		'name' => $name,
			    	'birthday' => $birthday,
			    	'postcode'=> $postcode,
			    	'address' => $address,
			    	'mobile' => $mobile,
			    	'income' => $income,
			    	'education' => $education,
			    	'school' => $school,
			    	'sex' => $sex
		    	 ]);

    	if($isUser){
			return json(['code'=>0, 'msg'=>'更新成功']);
		}else{
			return json(['code'=>1, 'msg'=>'更新失败，联系管理员']);
		}
    }

    public function password()
    {
        if(isMobile()){
            return $this->fetch('Mobile/Account/password');
        }else{
            return $this->fetch();
        }
    }

    public function ajaxPassword()
    {
        extract(request()->param());
        $isOld = User::where('id', session('auction_user')['id'])->where('password',md5(md5($passwordOld)))->find();
        if(!$isOld)
        	return json(['code'=>1, 'msg'=>'旧密码错误']);

    	$isOld->password = md5(md5($passwordNew));

    	if($isOld->save()){
			return json(['code'=>0, 'msg'=>'更新成功']);
		}else{
			return json(['code'=>1, 'msg'=>'更新失败，联系管理员']);
		}
    }

    public function order()
    {
    	$order = OrderModel::where('user_id', session('auction_user')['id'])->select();
    	$this->assign('orders', $order);

        if(isMobile()){
            return $this->fetch('Mobile/Account/order');
        }else{
            return $this->fetch();
        }
    }
    public function love(){
	    $user_id = session('auction_user')['id'];
	    $artworkids = Love::where(['user_id'=>$user_id])->column('artwork_id');
	   // pr($artworkids);
        $where = [];
        if($artworkids){
            $where['id'] = ['in',$artworkids];
        }
        $artwork = ArtworkModel::with('getartist')->where($where)->select();
        $artistIn = [];
        foreach ($artwork as $k => &$v) {
            $artistIn[] = $v['artist_id'];
            $in = explode(',', $v['photos']);
            $ins = [];
            for($i = 0; $i <count($in); $i++){
                $ins[] = Attachment::where('id',$in[$i])->value('path');
            }
            $v['img'] = $ins;
            $artwork[$k]['img'] = $ins;
        }
        $this->assign('artworks', $artwork);

        if(isMobile()){
            return $this->fetch('Mobile/Account/love');
        }else{
            return $this->fetch();
        }


    }
    public function address(){
        $address = [];
        $address = Address::where(['user_id'=>session('auction_user')['id']])->select();
//        pr($address[0]->toArray());
        $this->assign('address',$address);
        if(isMobile()){
            return $this->fetch('Mobile/Account/address');
        }else{
            return $this->fetch();
        }
    }
    public function addaddress(){
        $address = [];
        if(isMobile()){
            return $this->fetch('Mobile/Account/addaddress');
        }else{
            return $this->fetch();
        }
    }
    public function ajaxaddress()
    {

        $account = Address::create([
            'country' => request()->param('country'),
            'prov' => request()->param('prov'),
            'city' => request()->param('city'),
            'name' => request()->param('name'),
            'address1' => request()->param('address1'),
            'address2' => request()->param('address2'),
            'email' => request()->param('email'),
            'postcode' => request()->param('postcode'),
            'mobile' => request()->param('mobile'),
            'sendtime' => request()->param('sendtime'),
            'user_id' => session('auction_user')['id'],
        ]);
        if($account){
            return json(['code'=>0, 'msg'=>'新增成功']);
        }else{
            return json(['code'=>1, 'msg'=>'新增失败']);
        }
    }
    public function update(){
        if(isMobile()){
            return $this->fetch('Mobile/Account/update');
        }else{
            return $this->fetch();
        }
    }
    public function code(){
        $phone = request()->param('number');
        $code = rand(1111,9999);
        Cache::set('edit_code@'.$phone, $code, 2000);
        sendSms($phone,$code);
    }
    public function ajaxUpdate (){
        if(request()->isAjax()){
            extract(request()->param());
            $phone = request()->param('account');
            $code = intval(request()->param('code'));
            if($code === Cache::get('edit_code@'.$phone)){
                $isReg = User::where('mobile',$phone)->find();
                if($isReg) return json(['code'=>0, 'msg'=>'手机号已被使用，请更换手机号或重新登录']);
                $userModel = new User;
                $user = $userModel->save([
                    'mobile'=>$phone
                ],['id'=>session("auction_user")['id']]);

                if($user) {
                    return json(['code'=>1, 'msg'=>'账户已修改为：'.$phone.'请牢记您的新账户']);
                }else{
                    return json(['code'=>0, 'msg'=>'修改失败，请稍后重试']);
                }
            }else{
                return json(['code'=>0, 'msg'=>'验证码不匹配']);
            }
        }
    }
}